const express = require('express')
const path = require('path')
const { downloadBaiduVideo } = require('./downloadVideo')
const ejs = require('ejs')

const app = express()

app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')

app.use(express.json())
app.use(express.urlencoded())
app.use(express.static(__dirname + '/static'))

app.get('/', (req, res) => {
    res.render('index')
})

app.post('/parse', (req, res) => {
    let url_str = req.body.url_str
    if (url_str) {
        downloadBaiduVideo(url_str).then(
            function (result) {
                let {videoUrl, title} = result
                res.render('download', {url: videoUrl, title: title} )
            },
            function (error) {
                console.log(error)
            }
        )
    }
})

app.listen(4000, function () {
    console.log('service is listenning 4000')
})
